// https://www.luogu.com.cn/problem/P2324
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
int rad(); // read int
const int max_size = 10;

void show(int (*a)[max_size]);
int dx[] = {0, -2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {0, 1, 2, 2, 1, -1, -2, -2, -1};
int tar[max_size][max_size] = {{},
                               {0, 1, 1, 1, 1, 1},
                               {0, 2, 1, 1, 1, 1},
                               {0, 2, 2, 0, 1, 1},
                               {0, 2, 2, 2, 2, 1},
                               {0, 2, 2, 2, 2, 2}};
int now[max_size][max_size];
int n, maxd;
int check() {
    int back = 0;
    rf(i, 6) rf(j, 6) if (tar[i][j] != now[i][j]) back++;
    return back;
}

inline bool inside(int x, int y) { return 1 <= x && x <= 5 && 1 <= y && y <= 5; };
void dfs(int dep, int x, int y, int pre_dir) {
    // printf("dep: %d\n", dep);
    // show(now);
    if (dep >= maxd) return;
    int diff = check();
    if (diff == 0) throw dep - 1;
    if (dep + diff - 1 >= maxd) return;

    rf(i, 8) {
        if (i == pre_dir) continue;
        int nx = x + dx[i], ny = y + dy[i];
        if (!inside(nx, ny)) continue;
        swap(now[nx][ny], now[x][y]);
        dfs(dep + 1, nx, ny, (i + 4) % 8);
        swap(now[nx][ny], now[x][y]);
    }
}

void init(int &sx, int &sy, char *row, char *decoder) {
    rf(i, 5) {
        scanf("%s", row + 1);
        rf(j, 5) {
            now[i][j] = decoder[row[j]];
            if (row[j] == '*') sx = i, sy = j;
        }
    }
}

int main() {
    char row[10], decoder[130];
    decoder['1'] = 1, decoder['0'] = 2, decoder['*'] = 0;
    n = rad();
    rf(T, n) {
        int sx, sy;
        init(sx, sy, row, decoder);
        try {
            rf(i, 17) {
                maxd = i;
                dfs(1, sx, sy, 0);
            }
            throw -1;
        } catch (int ans) {
            printf("%d\n", ans);
        }
    }
}

int rad() {
    int back = 0, ch = 0, neg = 0;
    for (; ch < '0' || ch > '9'; ch = getchar())
        neg = ch == '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        back = (back << 1) + (back << 3) + (ch & 0xf);
    return (back ^ -neg) + neg;
}
void show(int (*a)[max_size]) {
    rf(i, 5) {
        rf(j, 5) printf("%d ", a[i][j]);
        puts("");
    }
    puts("");
}
/*
1
11111
01111
00111
0000*
00000

*/